const path = require('path')
const TimeStamp = new Date().getTime();
const CompressionWebpackPlugin = require('compression-webpack-plugin');

const isProdOrTest = process.env.NODE_ENV !== 'development';

function resolve(dir) {
    return path.join(__dirname, dir)
}

module.exports = {
    publicPath: './',
    outputDir: 'dist',
    assetsDir: 'static',
    filenameHashing: false,
    // pages:{
    //     index:{
    //         entry:'src/pages/index/index.js',
    //         template:'src/pages/index/index.html',
    //         filename:'/index.html',
    //         title:'',
    //         chunks:['chunk-vendors','chunk-common','index']
    //     }
    // }
    lintOnSave: false,
    // runtimeCompiler: false,
    // transpileDependencies: ['ant-design-vue'],
    productionSourceMap: false, // 设为false，既可以减少包大小，也可以加密源码
    // configureWebpack: (config) => {

    // },
    chainWebpack: (config) => {
        config.plugins.delete('prefetch'); //默认开启prefetch(预先加载模块)，提前获取用户未来可能会访问的内容，在首屏会一次性加载全部路由文件，所以要关闭
        if(isProdOrTest) {
            // 对超过10kb的文件进行gzip压缩
            config.plugin('compressionPlugin').use(
                new CompressionWebpackPlugin({
                    test: /\.(js|css|html)$/, // 匹配文件名
                    threshold: 10240,
                })
            )
            config.optimization.runtimeChunk({
                name: 'runtime'
            })
            config.optimization.splitChunks({
                chunks: 'all',
                cacheGroups: {
                    common: {
                        name: 'chunk-common', // 打包后的文件名
                        chunks: 'initial',
                        minChunks: 1,
                        maxInitialRequests: 5,
                        minSize: 0,
                        priority: 1,
                        reuseExistingChunk: true
                    },
                    vendors: { // vendor是导入的npm包
                        name: 'chunk-vendors',
                        test: /[\\/]node_modules[\\/]/,
                        chunks: 'initial',
                        maxSize: 600000,
                        maxInitialRequests: 20,
                        priority: 2,
                        reuseExistingChunk: true,
                        enforce: true
                    },
                    antDesignVue: { // 把antDesignVue从chunk-vendors.js提取出来
                        name: 'chunk-ant-design-vue',
                        test: /[\\/]node_modules[\\/]ant-design-vue[\\/]/,
                        chunks: 'initial',
                        maxSize: 600000,
                        priority: 3,
                        reuseExistingChunk: true,
                        enforce: true
                    },
                    JQuery: {
                        name: 'chunk-jquery',
                        test: /[\\/]node_modules[\\/]jquery[\\/]/,
                        chunks: 'initial',
                        maxSize: 600000,
                        priority: 4,
                        reuseExistingChunk: true,
                        enforce: true
                    },
                    antvG2: {
                        name: 'chunk-antvG2',
                        test: /[\\/]node_modules[\\/]@antv[\\/]g2[\\/]/,
                        chunks: 'initial',
                        maxSize: 600000,
                        priority: 5,
                        reuseExistingChunk: true,
                        enforce: true
                    },
                    eCharts: {
                        name: 'chunk-eCharts',
                        test: /[\\/]node_modules[\\/]echarts[\\/]/,
                        chunks: 'initial',
                        maxSize: 600000,
                        priority: 6,
                        reuseExistingChunk: true,
                        enforce: true
                    },
                    antvX6: {
                        name: 'chunk-antvX6',
                        test: /[\\/]node_modules[\\/]@antv[\\/]x6[\\/]/,
                        chunks: 'initial',
                        maxSize: 600000,
                        priority: 7,
                        reuseExistingChunk: true,
                        enforce: true
                    },
                    antvDataSet: {
                        name: 'chunk-antvDataSet',
                        test: /[\\/]node_modules[\\/]@antv[\\/]data-set[\\/]/,
                        chunks: 'initial',
                        maxSize: 600000,
                        priority: 8,
                        reuseExistingChunk: true,
                        enforce: true
                    },
                    excelJs: {
                        name: 'chunk-excelJs',
                        test: /[\\/]node_modules[\\/]exceljs[\\/]/,
                        chunks: 'initial',
                        maxSize: 600000,
                        priority: 9,
                        reuseExistingChunk: true,
                        enforce: true
                    },
                    fileSaver: {
                        name: 'chunk-fileSaver',
                        test: /[\\/]node_modules[\\/]file-saver[\\/]/,
                        chunks: 'initial',
                        maxSize: 600000,
                        priority: 10,
                        reuseExistingChunk: true,
                        enforce: true
                    },
                }
            })
        }
        // config.module
        //     .rule('eslint')
        //     .exclude
        //     .add('/Users/maybexia/Downloads/FE/community_built-in/src/lib')
        //     .end()
        config.module
            .rule('images')
            .use('url-loader')
            .tap(options => Object.assign(options, { limit: 2500 ,esModule: false}))
        const svgRule = config.module.rule('svg')
        svgRule.uses.clear()
        svgRule
            .use('vue-svg-loader')
            .loader('vue-svg-loader')
        config.resolve.alias.set("@", resolve("src"))
        config.resolve.alias.set("~", resolve("static"))
    },
    // css: {
    //     modules: false,
    //     extract: true,
    //     sourceMap: false,
    //     loaderOptions: {
    //         css: {

    //         },
    //         postcss: {

    //         }
    //     }
    // },
    devServer: {
        host: '0.0.0.0',
        port: 3000,
        https: false,
        open: true,
        proxy: {
            // 配置跨域处理 可以设置你想要代理的接口
            '/proxy': {
                target: 'http://10.1.42.55:8001/api2',
                // target: 'http://192.168.25.150:8000/',
                changeOrigin: true,
                pathRewrite: {
                    '^/proxy': ''
                }
            }     
        },
        before: app => {}
    },
    configureWebpack: {
        output: {
            filename: `js/[name].${TimeStamp}.js`,
            chunkFilename: `js/[name].${TimeStamp}.js`,
        },
        performance: {
            //入口起点的最大体积
            maxEntrypointSize: 50000000,
                //生成文件的最大体积
            maxAssetSize: 30000000,
        }
    },
    css: {
        modules: false,
        sourceMap: false,
        loaderOptions: {
            css: {

            },
            postcss: {

            }
        },
        extract: {
            ignoreOrder: true,
            filename: `css/[name].${TimeStamp}.css`,
            chunkFilename: `css/[name].${TimeStamp}.css`
        }
    },
    parallel: require('os').cpus().length > 1,
    pwa: {},
    pluginOptions: {

    },
}